/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import bean.Master;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HoangNam
 */
public class Converter<T> {

    public Converter() {
    }

    private T newInstance(Class<T> cl) {
        try {
            return cl.newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            System.out.println(">>> Converter: " + ex.getMessage());
        }
        return null;
    }

    private T setValueForObject(T obj, ResultSet rs) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] p = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor propertyDesc : beanInfo.getPropertyDescriptors()) {
                String propertyName = propertyDesc.getName();
//                Object value = propertyDesc.getReadMethod().invoke(obj);
                if (propertyName.matches("class") == false) {
                    try {
                        Object temp = obj;
                        Master cl = (Master) temp;
                        cl.setAttribute(propertyName, rs.getObject(propertyName));
                    } catch (SQLException ex) {
                        System.out.println(">>> Converter: " + ex.getMessage());
                    }
                }
            }
            return obj;
        } catch (IntrospectionException ex) {
            System.out.println(">>> Converter: " + ex.getMessage());
        }
        return null;
    }

    public T getObject(Class T, ResultSet rs) {
        try {
            while (rs.next()) {
                T obj = newInstance(T);
                obj = setValueForObject(obj, rs);
                return obj;
            }
        } catch (SQLException ex) {
            System.out.println(">>> Converter: " + ex.getMessage());
        }
        return null;
    }

    public List<T> getObjectList(Class T, ResultSet rs) {
        try {
            List<T> list = new ArrayList<T>();
            while (rs.next()) {
                T obj = newInstance(T);
                list.add(setValueForObject(obj, rs));
            }
            return list;
        } catch (SQLException ex) {
            System.out.println(">>> Converter: " + ex.getMessage());
        }
        return null;
    }

    public DefaultComboBoxModel convertListToComboBoxModel(List<T> list) {
        DefaultComboBoxModel model = new DefaultComboBoxModel(list.toArray());
        model.addElement("---Chọn---");
        model.setSelectedItem("---Chọn--");
        return model;
    }

    public DefaultTableModel convertListToTableModel(List<T> list, DefaultTableModel model) {
        for (T item : list) {
            model.addRow(setValueForVector(item));
        }
        return model;
    }

    private Vector setValueForVector(T obj) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] p = beanInfo.getPropertyDescriptors();
            Vector vt = new Vector();
            try {
                for (PropertyDescriptor propertyDesc : beanInfo.getPropertyDescriptors()) {
                    String propertyName = propertyDesc.getName();
                    if (propertyName.matches("class") == false) {
                        Object value = propertyDesc.getReadMethod().invoke(obj);
                        vt.add(value);
                    }
                }
                return vt;
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                System.out.println(">>> Converter: " + ex.getMessage());
            }
        } catch (IntrospectionException ex) {
            System.out.println(">>> Converter: " + ex.getMessage());
        }
        return null;
    }
}
